<?php
class GroupsController extends AppController {
	
	var $name = 'Groups';
	var $components = array ('Auth' ); // Not necessary if declared in your app controller
	var $layout = 'admin';
	var $helpers = array('Javascript');
	
	public function index() {
		$groups = $this->Group->find ( 'all' );
		$this->set ( 'groups', $groups );
	}
	
	public function delete($id = NULL) {
		if ($id) {
			if ($this->Dictionary->delete ( $id )) {
				$this->Session->setFlash ( 'Dictionary item deleted successfuly!' );
				$this->redirect ( array ('action' => 'index' ) );
			} else {
				$this->Session->setFlash ( 'Failed! Please try again or contact to administrator.' );
				$this->redirect ( array ('action' => 'index' ) );
			}
		
		} else {
			$this->Session->setFlash ( 'Failed! No such dictionary item.' );
			$this->redirect ( array ('action' => 'index' ) );
		
		}
	}
	
	public function add() {
		if (! empty ( $this->data )) {
			
			$saveable = array (	"key" => $this->data ['Dictionary'] ['key'] ,
								"eng" => $this->data ['Dictionary'] ['eng'] ,
			 					"rus" => $this->data ['Dictionary'] ['rus'] ,
								"arm" => $this->data ['Dictionary'] ['arm'] ,
								"group" => $this->data ['Dictionary'] ['group'] );
			
			$this->Dictionary->create ();
			
			if ($this->Dictionary->save ( $saveable )) {
				$this->Session->setFlash ( 'Dictionary item added successfuly!' );
				$this->redirect ( array ('action' => 'index' ) );
			} else {
				$this->Session->setFlash ( 'Failed to add dictionary item!' );
			}
		} else {
			$allContents = $this->Dictionary->find ( 'all', array ('fields' => 'DISTINCT group') );
			$contentsUrls ['-1'] = '--select--';
			$contentsUrls ['-2'] = '--other--';
			
			foreach ( $allContents as $allContent ) {
				$contentsUrls [APP_ROOT . '/' . $allContent ['Dictionary'] ['group']] = $allContent ['Dictionary'] ['group'];
			}
			$this->set ( 'contentsUrls', $contentsUrls );
		}
	}
	
	public function edit($id) {
		if (! empty ( $this->data )) {
			$this->Dictionary->id = $id;
			if ($this->Dictionary->save ( $this->data )) {
				$this->Session->setFlash ( 'Dictionary item edited successfuly!' );
				$this->redirect ( array ('action' => 'index' ) );
			} else {
				$this->Session->setFlash ( 'Failed to edit dictionary item!' );
			}
		} else {
			$this->data = $this->Dictionary->findById ( $id );
		}
	}
	
	///////////////////////////////////////////
	public function __($key) {
		$this_lang = 'ru';
		$this->Dictionary->find ( 'all', array ('conditions' => array ('key' => $key ), 'field' => $this_lang ) );
		// return ;
	}

}

?>